<?

/**
 * generates sql insert info for smutty
 *
 */

class Sql {

	var $f = null;

	/**
	 *  Doclet constructor.
	 *
	 *  @param RootDoc rootDoc
	 *
	 */

	function sql( &$rootDoc ) {
		$this->_f = fopen( '../../smartysql.sql', 'w' );
		foreach ($rootDoc->packages() as $package)
			$this->classDoc($package->allClasses());
		fclose( $this->_f );
	}

	function w( $s ) {
		fwrite( $this->_f, $s );
	}

	/** Output methodDoc
	 *
	 * @param MethodDoc[] methods
	 */
	function methodDoc( $methods ) {
		if ( $methods )
			foreach ( $methods as $method ) {

				// get method description text
				$text = '<p><b>' . $method->name() . '</b>' . $method->flatSignature();
				if ( $textTag = $method->tags('@text') )
					$text .= '<p>' . $textTag->_text . '</p>';
				$text .= $this->_processTags( $method->tags() );

			}
	}

	function _processTags(&$tags)
    {
		$tagString = '';
		foreach ($tags as $key => $tag) {
			if ($key != '@text') {
				if (is_array($tag)) {
                    $hasText = FALSE;
                    foreach ($tag as $key => $tagFromGroup) {
                        if ($tagFromGroup->text() != '') {
                            $hasText = TRUE;
                        }
                    }
                    if ($hasText) {
                        $tagString .= '<dt>'.$tag[0]->displayName().":</dt>\n";
                        foreach ($tag as $tagFromGroup) {
                            $tagString .= '<dd>'.$tagFromGroup->text()."</dd>\n";
                        }
                    }
				} else {
					$text = $tag->text();
					if ($text != '') {
						$tagString .= '<dt>'.$tag->displayName().":</dt>\n";
						$tagString .= '<dd>'.$text."</dd>\n";
					} elseif ($tag->displayEmpty()) {
						$tagString .= '<dt>'.$tag->displayName().".</dt>\n";
					}
				}
			}
		}
        if ($tagString) {
            return "<dl>\n" . $tagString . "</dl>\n";
        }
	}

	/** Output classDoc
	 *
	 * @param ClassDoc[] classes
	 */
	function classDoc(&$classes)
    {
		$this->depth++;
		if ($classes) {
			foreach ($classes as $class)
				$this->methodDoc($class->methods());
		}
		$this->depth--;
	}
	
}

?>
